from sqlmodel import SQLModel, Field
from typing import Optional
from datetime import datetime

# Database model
class ApiLog(SQLModel, table=True):
    __tablename__ = "t_api_log" # 数据库表名（可选，不写默认会用类名小写作为表名）

    id: Optional[int] = Field(default=None, primary_key=True, index=True)
    uid: Optional[int] = Field(default=0, index=True, description="用户ID")
    path: str = Field(max_length=255, description="请求路径")
    method: str = Field(max_length=10, description="请求方法")
    status_code: int = Field(default=200, description="响应状态码")
    process_time: float = Field(default=0.0, description="处理时间（秒）")
    request_body: Optional[str] = Field(default=None, description="请求体")
    response_body: Optional[str] = Field(default=None, description="响应体")
    ip_address: Optional[str] = Field(default=None, description="IP地址")
    created_at: datetime = Field(default_factory=datetime.now, description="创建时间")
